<template>
  <div class="container" style="margin-top:15px">
    <el-form
      label-width="120px"
      :model="examinerInfo"
      status-icon
      ref="examinerInfo"
    >
      <el-form-item
        label="审查员名称"
        prop="examinerName"
        :rules="[{ required: true, message: '请输入名称', trigger: 'blur' }]"
      >
        <el-input placeholder="例如:王静" v-model="examinerInfo.examinerName" />
      </el-form-item>
      <el-form-item
        label="邮箱账号"
        class="input-prepend restyle"
        prop="examinerEmail"
        :rules="[
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' },
        ]"
      >
        <el-input
          type="text"
          placeholder="例如:123456@qq.com"
          v-model="examinerInfo.examinerEmail"
        />
      </el-form-item>
      <el-form-item label="审查员性别">
        <el-radio v-model="examinerInfo.examinerSex" label="true">男</el-radio>
        <el-radio v-model="examinerInfo.examinerSex" label="false">女</el-radio>
      </el-form-item>
      <!-- <el-form-item label="生日">
        <el-date-picker
          v-model="examinerInfo.examinerBirth"
          style="width: 205px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="生日">
        <el-date-picker
          size="mini"
          v-model="examinerInfo.examinerBirth"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-form-item label="密码默认为:">
        <span style="color: gray">123456</span>
      </el-form-item>
      <el-form-item label="审查员头像">
        <!-- 头衔缩略图 -->
        <pan-thumb
          style="width: 100px; height: 100px; margin: 10px 0 0 10px"
          :image="
            examinerInfo.examinerAvatar
              ? examinerInfo.examinerAvatar
              : 'https://edu-lzy0916.oss-cn-guangzhou.aliyuncs.com/2021/11/04/a02bfd138b0c4858925dae102008ed2bfile.png'
          "
        />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >更换头像
        </el-button>
        <!--
      v-show：是否显示上传组件
      :key：类似于id，如果一个页面多个图片上传控件，可以做区分
      :url：后台上传的url地址
      @close：关闭上传组件
      @crop-upload-success：上传成功后的回调 -->
        <!-- field的值要对应后端界面MultipartFile对应的名字 否则会上传失败 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="'/eduOss/fileOss'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="addexaminerInfoList"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import examinerApi from "@/api/examiner";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";
import md5 from "md5-js";
export default {
  components: {
    ImageCropper,
    PanThumb,
  },
  data() {
    return {
      examinerInfo: {
        examinerId: "",
        examinerName: "张全" + parseInt(Math.random() * 10000000),
        examinerPwd: "123456",
        examinerSex: "true",
        examinerBirth: "1990-09-09 00:00:00",
        examinerAvatar:
          "https://edu-lzy0916.oss-cn-guangzhou.aliyuncs.com/2021/11/04/a02bfd138b0c4858925dae102008ed2bfile.png",
        // examinerEmail: "123456@qq.com",
        examinerEmail: "",
        isDisabled: false,
        isDeleted: false,
      },
      //上传弹框组件是否显示
      imagecropperShow: false,
      imagecropperKey: 0, //上传组件key的值
      saveBtnDisabled: false, //按钮是否禁用 重复是禁用
    };
  },
  //created()方法只执行一次，所以再调整页面就执行了一次，切换路由（切换页面）也就起不到效果
  created() {},
  watch: {
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
    checkEmail(rule, value, callback) {
      //邮箱校验
      if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(value)) {
        return callback(new Error("邮箱地址格式不正确"));
      }
      return callback();
    },
    close() {
      //关闭上传弹框的方法
      this.imagecropperShow = false;
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    cropSuccess(data) {
      this.imagecropperShow = false;
      //上传之后接口返回图片地址 data.url等价于response.data.url 里面做了封装
      this.examinerInfo.examinerAvatar = data.url;
      // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
      this.$message({
        message: "上传头像成功",
        type: "success",
      });
    },
    //添加审查员的方法
    addexaminerInfoList() {
      this.$refs.examinerInfo.validate((flag, obj) => {
        if (!flag) {
          // this.$message({
          //   message: "请输入完整",
          //   type: "warning",
          // });
          return;
        }
        this.examinerInfo.examinerPwd = md5("123456");
        examinerApi.toAddExaminer(this.examinerInfo).then((response) => {
          //提示信息
          this.$message({
            type: "success",
            message: "添加成功!",
          });
          //回到视频审查员列表页面 路由跳转
          this.$router.push({ path: "/examinerList" });
        });
      });
    },
  },
};
</script>

<style>
</style>
